package com.weixing.mall.provider.controller.sys;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.vo.SysDictVo;
import com.weixing.mall.provider.model.dto.sys.SysDictDto;
import com.weixing.mall.provider.model.query.SysDictQuery;
import com.weixing.mall.provider.service.IDictService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/7/24 10:04
 * @Modified By:
 */
@RestController
@RequestMapping("dict")
@Api(value = "数据字典APi", tags = "数据字典")
public class SysDictController extends BaseController {


    @Autowired
    private IDictService sysDictService;

    @ApiOperation(value = "加载数据字典")
    @GetMapping("")
    public Result list(SysDictQuery sysDictQuery) {
        return ResultUtil.success(sysDictService.selectOfPage(initMpPage(),sysDictQuery));
    }

    @NoRepeatSubmit
    @ApiOperation(value = "保存数据字典")
    @PostMapping("save")
    public Result save(@Validated SysDictDto dictDto) {
        sysDictService.save(dictDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除数据字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典id", paramType = "path", dataType = "long")
    })
    @PostMapping("del/{id}")
    public Result del(@PathVariable long id) {
        return sysDictService.removeById(id)  ? ResultUtil.success() : ResultUtil.error();
    }

    @ApiModelProperty(value = "查询 select下拉框")
    @GetMapping("form")
    public Result form(SysDictQuery sysDictQuery){
        return ResultUtil.success(sysDictService.selectDictFormVoList(sysDictQuery));
    }

    @GetMapping("getDictByCode")
    public Result<List<SysDictVo>> getDictByCode(String dictGroupCode) {
        List<SysDictVo> list = sysDictService.getDictByCode(dictGroupCode);
        return  ResultUtil.success(list);
    }
}
